package usf.gwt.ui.bootstrap.client.core;

import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.DOM;

public final class JqueryUtils {
	
	
	public static final boolean isActive(Element e) {
		return hasClass(e, Constants.BOOTSTRAP_ACTIVE);
	}
	public static final void activate(Element e, boolean add) {
		switchClass(e, Constants.BOOTSTRAP_ACTIVE, add);
	}
	public static final void activateUnique(Element e, boolean add) {
		switchClassUnique(e, Constants.BOOTSTRAP_ACTIVE, add);
	}
	public static final boolean isStyleEnabled(Element e) {
		return hasClass(e, Constants.BOOTSTRAP_DISABLED);
	}
	public static final void setStyleEnabled(Element e, boolean value) {
		switchClass(e, Constants.BOOTSTRAP_DISABLED, !value);
	}
	public static final boolean isFormControlEnabled(Element e) {
		return hasAttrib(e, Constants.BOOTSTRAP_DISABLED);
	}
	public static final void setFormControlEnabled(Element e, boolean value) {
		switchAttribute(e, Constants.BOOTSTRAP_DISABLED, !value);
	}
	public static final Enum<?> hasClass(Element e, Enum<?>[] types){
		for(Enum<?> t : types)  
			if(hasClass(e, ((StyleEnum)t).getValue())) return t;
		return null;
	}
	public static final void switchClass(Element e, Enum<?>[] types, Enum<?> type){
		for(Enum<?> t : types)  switchClass(e, ((StyleEnum)t).getValue(), false);
		switchClass(e, ((StyleEnum)type).getValue(), true);
	}
	public static final void collapse(Element e, boolean collapse) {
        if (collapse) switchClass(e, Constants.BOOTSTRAP_COLLAPSE_IN, Constants.BOOTSTRAP_COLLAPSE);
        else switchClass(e, Constants.BOOTSTRAP_COLLAPSE, Constants.BOOTSTRAP_COLLAPSE_IN);
    }
	public static final void collapse(Element e, Element target, boolean collapse) {
        if (collapse){
        	switchClass(target, Constants.BOOTSTRAP_COLLAPSE_IN, Constants.BOOTSTRAP_COLLAPSE);
        	e.addClassName(Constants.BOOTSTRAP_COLLAPSED);
        }
        else {
        	switchClass(target, Constants.BOOTSTRAP_COLLAPSE, Constants.BOOTSTRAP_COLLAPSE_IN);
        	e.removeClassName(Constants.BOOTSTRAP_COLLAPSED);
        }
    }

	public static final void attachCollapser(Element elem, Element target) {
		if(target.getId()==null || target.getId().isEmpty())
			target.setId("ui-" + DOM.createUniqueId());
        elem.setAttribute(Constants.ATTRIB_DATA_TARGET, '#' + target.getId());
        elem.setAttribute(Constants.ATTRIB_DATA_TOGGLE, Constants.BOOTSTRAP_COLLAPSE);
    	switchClass(target, Constants.BOOTSTRAP_COLLAPSE, Constants.BOOTSTRAP_COLLAPSE_IN);
	}
	
	public static final native void switchClassUnique(final Element e, final String c, final boolean a) /*-{
		$wnd.$(e).parent().find(e.tagName).removeClass(c);
		a && $wnd.$(e).addClass(c);
	}-*/;
	public static final native boolean hasClass(Element e, String c)/*-{
		return $wnd.$(e).hasClass(c);
	}-*/;
	public static final native boolean hasAttrib(Element e, String c)/*-{
		return $wnd.$(e).is('['+ c +']');
	}-*/;
	public static final native void switchClass(final Element e, final String oldClass, final String newClass)/*-{
		$wnd.$(e).removeClass(oldClass).addClass(newClass);
	}-*/;
	public static final native void switchClass(final Element e, final String className, final boolean a)/*-{
		a && $wnd.$(e).addClass(className);
		a || $wnd.$(e).removeClass(className);
	}-*/;
	
	public static final native void switchAttribute(final Element e, final String attr, final boolean a)/*-{
		$wnd.$(e).attr(attr, a ? '' : null);
	}-*/;
	
	public static final native void jqueryConflict()/*-{
		$wnd.$.noConflict();
	}-*/;
	
}
